package com.example.duanjunjie.learn.wk;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.duanjunjie.learn.R;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by duanjunjie on 2018/7/21.
 */
public class WKActivity extends AppCompatActivity implements WKMessageListener {

    private Button button;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wk);
        button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WKNoticeManager2.getInstance().onWKConnectionEstablished();
                for (int i = 0; i < 20; i++) {
                    try {
                        JSONObject jsonObject = new JSONObject("{\n" +
                                "    \"ctype\": 2,\n" +
                                "    \"info\": {\n" +
                                "        \"my_score\": \"192\",\n" +
                                "        \"roomid\": \"1947442\",\n" +
                                "        \"stage\": \"2\",\n" +
                                "        \"stage_score\": \"199\",\n" +
                                "        \"user_current\": \"5\",\n" +
                                "        \"user_total\": \"10\"\n" +
                                "    }\n" +
                                "}");
                        WKNoticeManager2.getInstance().handleWKNoticeMessage(new WKNoticeMessagePacket(i, jsonObject));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                WKNoticeManager2.getInstance().onWKConnectionLost();
            }
        });
        WKNoticeManager2.getInstance().registerListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WKNoticeManager2.getInstance().unregisterListener(this);
        // WKNoticeManager.getInstance().release();
    }

    @Override
    public void onReceiveWuKongMessage(WKNoticeMessageBase message) {
        if (message.data instanceof WkMessage13) {
            Log.i("djj", "wkMessage13|ctype:" + ((WkMessage13) message.data).ctype + "|thread:" + Thread.currentThread().getName());
        }


    }

    @Override
    public void onWKConnectLost() {
        Log.i("djj", "onWKConnectLost|thread:" + Thread.currentThread().getName());
    }

    @Override
    public void onWKConnectEstablished() {
        Log.i("djj", "onWKConnectEstablished|thread:" + Thread.currentThread().getName());
    }

    @Override
    public WKNoticeMessageParser renderParser() {
        return new WKNoticeMessageParser() {
            @Override
            public boolean canParse(WKNoticeMessagePacket packet) {
                return packet.type == 13;
            }

            @Override
            public IWKNoticeMessageData parse(WKNoticeMessagePacket packet) {
                WkMessage13 wkMessage13 = new WkMessage13();
                wkMessage13.read(packet.data);
                Log.i("djj", "parse|thread:" + Thread.currentThread().getName());
                return wkMessage13;
            }
        };
    }


}
